//-----------------------------------------------------------------------
// <copyright file="SupplierDAO.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
	
using System;
using System.Data;
using MIMS.WRS.Entities;
using MyFramework;
using MyFramework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace MIMS.WRS.Data
{	
	public class SupplierDAO : SupplierDAOBase
	{
		public SupplierDAO()
		{
		}

		public override SupplierCollection GetSuppliers(SupplierColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
		{            
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spSuppliersGetList");
                
				dbCommandWrapper.AddInParameter("@OrderBy", DbType.AnsiString, orderBy.ToString());
				dbCommandWrapper.AddInParameter("@OrderDirection", DbType.AnsiString, orderDirection.ToString());
				dbCommandWrapper.AddInParameter("@Page", DbType.Int32, page);
				dbCommandWrapper.AddInParameter("@PageSize", DbType.Int32, pageSize);
				dbCommandWrapper.AddOutParameter("@TotalRecords", DbType.Int32, 4);
                
				SupplierCollection supplierCollection = new SupplierCollection();
				using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
				{
					while (reader.Read())
					{
						Supplier supplier = CreateSupplierFromReader(reader);
						if (!reader.IsDBNull(reader.GetOrdinal("ContainerLoading"))) supplier.ContainerLoading = (string)reader["ContainerLoading"];
						supplierCollection.Add(supplier);
					}
					reader.Close();
				}
				totalRecords = (int)dbCommandWrapper.GetParameterValue("@TotalRecords");
				return supplierCollection;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessGetSuppliersException, ex);
			}
		}

		public virtual SupplierCollection GetSuppliers(int conPointID)
		{            
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spSuppliersGetListByConPoint");
                
				dbCommandWrapper.AddInParameter("@ConPointID", DbType.Int32, conPointID);
			                
				SupplierCollection supplierCollection = new SupplierCollection();
				using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
				{
					while (reader.Read())
					{
						Supplier supplier = CreateSupplierFromReader(reader);						
						supplierCollection.Add(supplier);
					}
					reader.Close();
				}
				
				return supplierCollection;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessGetSuppliersException, ex);
			}
		}

		public virtual SupplierCollection GetAllSuppliers()
		{            
			try
			{
				Database database = DatabaseFactory.CreateDatabase();
				DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spSuppliersGetListAll");
           			                
				SupplierCollection supplierCollection = new SupplierCollection();
				using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
				{
					while (reader.Read())
					{
						Supplier supplier = CreateSupplierFromReader(reader);
						supplierCollection.Add(supplier);
					}
					reader.Close();
				}
				
				return supplierCollection;
			}
			catch (Exception ex)
			{
				// log this exception
				Logger.Error(ex);
				// wrap it and rethrow
				throw new DataAccessException(SR.DataAccessGetSuppliersException, ex);
			}
		}
	}
}
